package com.qiyun.framework.helper;

import android.content.Context;
import android.os.Environment;
import android.text.TextUtils;


import com.qiyun.framework.utils.SystemUtils;

import java.io.File;
import java.io.IOException;

/**
 * Created by Tiger on 16/2/29.
 */
public class StorageHelper {
    private static final String EXTERNAL_DIR_PATH = Environment.getExternalStorageDirectory().getAbsolutePath();
    private static String PARENT_SHORT_PATH = "/SmartGuide";
    private static String sSDCardStorePath;

    public StorageHelper() {
    }

    public static String getSDCardStorePath(Context context) {
        if(TextUtils.isEmpty(sSDCardStorePath)) {
            String packageName = context.getPackageName();
            int lastPoint = packageName.lastIndexOf(46);
            String shortPackageName = lastPoint >= 0?packageName.substring(lastPoint + 1):packageName;
            sSDCardStorePath = EXTERNAL_DIR_PATH + PARENT_SHORT_PATH + "/" + shortPackageName;
        }

        return sSDCardStorePath;
    }

    public static void setSDCardStorePath(String path) {
        if(!TextUtils.isEmpty(path)) {
            sSDCardStorePath = path;
        }

    }

    public static File getSDCardStoreDir(Context context) {
        File file = new File(getSDCardStorePath(context));
        file.mkdirs();
        return file;
    }

    public static File getExternalCacheDir(Context context) {
        File file = null;
        if(SystemUtils.hasFroyo()) {
            file = context.getExternalCacheDir();
        }

        if(file == null) {
            String noMediaFile = "/Android/data/" + context.getPackageName();
            file = new File(EXTERNAL_DIR_PATH + noMediaFile);
        }

        file.mkdirs();
        File noMediaFile1 = new File(file.getAbsolutePath() + "/.nomedia");
        if(!noMediaFile1.exists()) {
            try {
                noMediaFile1.createNewFile();
            } catch (IOException var4) {
                var4.printStackTrace();
            }
        }

        return file.isDirectory()?file:null;
    }

    public static String getExternalCachePath(Context context) {
        File cacheDir = getExternalCacheDir(context);
        return cacheDir == null?"":cacheDir.getAbsolutePath();
    }

    public static String getExternalImageCachePath(Context context) {
        return getExternalImageCacheDir(context).getAbsolutePath();
    }

    public static File getExternalImageCacheDir(Context context) {
        File file = new File(getExternalCachePath(context) + File.separator + "image");
        if(!file.isDirectory()) {
            if(file.exists()) {
                file.delete();
            }

            file.mkdirs();
        } else if(!file.exists()) {
            file.mkdirs();
        }

        return file;
    }

    public static String getExternalObjectCachePath(Context context) {
        return getExternalObjectCacheDir(context).getAbsolutePath();
    }

    public static File getExternalObjectCacheDir(Context context) {
        File file = new File(getExternalCachePath(context) + File.separator + "object");
        if(!file.isDirectory()) {
            if(file.exists()) {
                file.delete();
            }

            file.mkdirs();
        } else if(!file.exists()) {
            file.mkdirs();
        }

        return file;
    }

    public static String getExternalDataCachePath(Context context) {
        return getExternalDataCacheDir(context).getAbsolutePath();
    }

    public static File getExternalDataCacheDir(Context context) {
        File file = new File(getExternalCachePath(context) + File.separator + "data");
        if(!file.isDirectory()) {
            if(file.exists()) {
                file.delete();
            }

            file.mkdirs();
        } else if(!file.exists()) {
            file.mkdirs();
        }

        return file;
    }

    public static String getExternalAudioCachePath(Context context) {
        return getExternalAudioCacheDir(context).getAbsolutePath();
    }

    public static File getExternalAudioCacheDir(Context context) {
        File file = new File(getExternalCachePath(context) + File.separator + "audio");
        if(!file.isDirectory()) {
            if(file.exists()) {
                file.delete();
            }

            file.mkdirs();
        } else if(!file.exists()) {
            file.mkdirs();
        }

        return file;
    }

    public static String getExternalVideoCachePath(Context context) {
        return getExternalVideoCacheDir(context).getAbsolutePath();
    }

    public static File getExternalVideoCacheDir(Context context) {
        File file = new File(getExternalCachePath(context) + File.separator + "video");
        if(!file.isDirectory()) {
            if(file.exists()) {
                file.delete();
            }

            file.mkdirs();
        } else if(!file.exists()) {
            file.mkdirs();
        }

        return file;
    }
}
